package com.sy.service.impl;

import com.sy.dto.JsonResult;
import com.sy.dto.UserRankingInfo;
import com.sy.dto.UserWeekRankingInfo;
import com.sy.entity.BlogExample;
import com.sy.entity.User;
import com.sy.entity.UserRanking;
import com.sy.mapper.BlogMapper;
import com.sy.mapper.UserMapper;
import com.sy.mapper.UserRankingMapper;
import com.sy.service.RankingListService;
import com.sy.utils.HttpUtils;
import com.sy.utils.RedisClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Tuple;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.sy.constant.BusinessConstant.*;

/**
 * Description:
 * date: 2021/1/19 14:48
 *
 * @author:
 * @since JDK 1.8
 */
@Service
public class RankingListServiceImpl implements RankingListService {
    @Autowired
    private RedisClient redisClient;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private HttpUtils httpUtils;
    @Autowired
    private UserRankingMapper userRankingMapper;

    @Override
    public JsonResult getWeekRankingList() {
        //创建用户周排行对象集合
        List<UserWeekRankingInfo> userWeekRankingInfos = new ArrayList<>();
        //从redis中获取周排行排名前十的用户和分值
        Set<Tuple> tuples = redisClient.zrevrangeWithScores(REDIS_BLOG_USER_WEEK_RANKING_KEY, 0, 9);
        for(Tuple tuple:tuples){
            //获取综合指标
            double score = tuple.getScore();
            //获取用户id
            Integer userId = Integer.parseInt(tuple.getElement());
            //创建用户周排行对象
            UserWeekRankingInfo userWeekRankingInfo = new UserWeekRankingInfo();
            userWeekRankingInfo.setScore(score);
            //查询该用户
            User user = userMapper.selectByPrimaryKey(userId);
            userWeekRankingInfo.setUser(user);
            //查询该用户的总点赞数
            int thumbsCount = blogMapper.sumThumbsCountByUserId(userId);
            userWeekRankingInfo.setThumbCount(thumbsCount);
            userWeekRankingInfos.add(userWeekRankingInfo);
        }
        //根据集合中分值大小排序
        userWeekRankingInfos = userWeekRankingInfos.stream()
                .sorted(Comparator.comparing(UserWeekRankingInfo::getScore).reversed())
                .collect(Collectors.toList());
        return httpUtils.buildSuccess(200,userWeekRankingInfos);
    }

    @Override
    public JsonResult getUserRankingInfo(Integer userId) {
        //创建用户总排名对象
        UserRankingInfo userRankingInfo = new UserRankingInfo();
        //获取数据库中用户的排名数据
        UserRanking userRanking = userRankingMapper.selectByUserId(userId);
        //获取该用户的访问量
        String userVisitCount = redisClient.hget(REDIS_USER_CLICK_HASH_KEY, String.valueOf(userId));
        userRanking.setClickCount(Integer.parseInt(userVisitCount));
        userRankingInfo.setUserRanking(userRanking);
        //获得该用户的周排名
        Long weekRanking = redisClient.zrevrank(REDIS_BLOG_USER_WEEK_RANKING_KEY, String.valueOf(userId));
        userRankingInfo.setWeekRanking(weekRanking);
        //获得该用户的总排名
        Long totalRanking = redisClient.zrevrank(REDIS_BLOG_USER_TOTAL_RANKING_KEY, String.valueOf(userId));
        userRankingInfo.setTotalRanking(totalRanking);
        return httpUtils.buildSuccess(200,userRankingInfo);
    }
}
